/* 
请求地址1：https://autumnfish.cn/search
请求地址2：https://autumnfish.cn/song/url
请求地址3：https://autumnfish.cn/song/detail
请求地址4：https://autumnfish.cn/comment/hot?type=0
请求地址5：https://autumnfish.cn/mv/url

请求方法1/2/3/4/5：get
请求参数1：keywords(查询关键字)
请求参数2：id(歌曲id)
请求参数3：ids(歌曲id)
请求参数4：id(歌曲id，type=0固定值)
请求参数4：id(mvid，为0则没有mv)

响应内容1：歌曲搜索结果
响应内容2：歌曲url
响应内容3：封面信息
响应内容4：热门评论
响应内容4：mv地址
*/

//加载数据

let hht = new Vue({
  el: "#hht",
  data: {
    keywords: "",
    history: [],
    noHistory: true,
    like: {
      name: [],
      ids: [],
    },
    noLike: true,
    musicList: [],
    musicUrl: "",
    picUrl: "",
    hotComments: [],
    comments: false,
    heart: false,
    isPlaying: false,
    isPausing: false,
    song: {},
    mvUrl: "",
    mv: false,
    m800: false,
    m1600: false,
    playLogo: false,
  },
  methods: {
    searchMusic: function () {
      let self = this;
      this.history.push(this.keywords);
      this.noHistory = false;
      axios.get("https://autumnfish.cn/search?keywords=" + this.keywords).then(
        function (response) {
          console.log(response.data.result.songs);
          self.musicList = response.data.result.songs;
          //翻页
          self.m1600 = false;
          self.m800 = true;
        },
        function (err) {
          console.log(err);
        }
      );
    },

    //设置keywords搜索歌曲
    searchHotMusic: function (key) {
      this.keywords = key;
      this.searchMusic();
    },

    //播放音乐，并获取细节信息，参数为音乐id
    playMusic: function (musicId) {
      let self = this;

      //根据歌曲id,得到url
      axios.get("https://autumnfish.cn/song/url?id=" + musicId).then(
        function (response) {
          self.musicUrl = response.data.data[0].url;
          self.musicUrl ? "" : alert("暂时无法播放，换一首吧");
          if (self.musicUrl) {
            self.m800 = false;
            self.m1600 = true;
          }
        },
        function (err) {
          console.log(err);
        }
      );

      //根据歌曲id,得到picUrl
      axios.get("https://autumnfish.cn/song/detail?ids=" + musicId).then(
        function (response) {
          self.song = response.data.songs[0];
          console.log(self.song);

          self.picUrl = response.data.songs[0].al.picUrl;
        },
        function (err) {
          console.log(err);
        }
      );

      //根据歌曲id,type=0固定值，得到p热评
      axios.get("https://autumnfish.cn/comment/hot?type=0&id=" + musicId).then(
        function (response) {
          //   console.log(response);
          self.hotComments = response.data.hotComments;
          if (!self.hotComments.length) {
            self.comments = true;
          }
        },
        function (err) {
          console.log(err);
        }
      );

      // 判断是否为收藏歌曲
      this.heart = false;
      this.like.ids.includes(musicId) && (this.heart = true);
    },

    //播放,控制动画
    play: function () {
      this.isPlaying = true;
      this.isPausing = false;
      this.playLogo = true;
    },

    //暂停，控制动画
    pause: function () {
      this.isPausing = true;
    },

    //播放mv
    playMV: function (mvid) {
      this.mv = true;
      let self = this;
      axios.get("https://autumnfish.cn/mv/url?id=" + mvid).then(
        function (response) {
          self.mvUrl = response.data.data.url;
          document.querySelector(".video").style.cssText = "z-index: 1 ";
          let a = document.querySelector("audio");
          a.pause();
          self.pause();
          console.log(self.mvUrl);
        },
        function (err) {
          console.log(err);
        }
      );
    },

    //关闭mv
    closeMV: function () {
      this.mv = false;
      let v = document.querySelector("video");
      v.pause();
    },

    //回到首页
    backHome: function () {
      this.m800 = false;
      this.m1600 = false;
    },

    //回到播放页
    backDetail: function () {
      this.m800 = false;
      this.m1600 = true;
    },

    // 添加或移除喜欢
    addLike: function (sname, sid) {
      if (this.like.ids.includes(sid)) {
        let self = this;
        this.heart = false;
        this.like.ids = this.like.ids.filter((item, index) => {
          if (item == sid) {
            self.like.name.splice(index, 1);
          }
          return item != sid;
        });
        this.heart = false;
      } else {
        this.heart = true;
        this.like.name.push(sname);
        this.like.ids.push(sid);
      }
      this.like.name.length ? (this.noLike = false) : (this.noLike = true);
    },

    //播放收藏歌曲
    likeSong: function (item, index) {
      this.keywords = item;
      this.playMusic(this.like.ids[index]);
    },
  },
});
